package org.pi.common.geom

class Line(val p1: Point, val p2: Point) extends Straight(p1.homogeneousCoordinates crossprod p2.homogeneousCoordinates) {
  val length = (p2-p1).length()
  val dirVector = (p2-p1)/length
  def signedDistanceOnLine(p: Point) = ((p-p1) dotprod dirVector)
  override def distance(p: Point) = {
    val pos = signedDistanceOnLine(p)
    if (pos<0) p1 distance p
    else if (pos>length) p2 distance p
    else super.distance(p)
  }
}

object Line {
  def apply(p1: Point, p2: Point) = new Line(p1,p2)
}

object LineMain {
  def main(args: Array[String]) {
    var s1 = Line( Point(1,1), Point(1,10) )
    println("dist s1 (0,-10)="+(s1 distance Point(0,-10))+", must be large")
    println("dist s1 (0,0)="+(s1 distance Point(0,0))+", must be 1.41..")
    println("dist s1 (0,1)="+(s1 distance Point(0,1))+", must be 1.0")
    println("dist s1 (1,12)="+(s1 distance Point(1,12))+", must be 2.0")
  }
}